package cn.sdormitory.component.bindValue.query.mapping;


import cn.sdormitory.basedata.service.BStudentService;
import cn.sdormitory.common.utils.ContainerUtil;
import cn.sdormitory.component.bindValue.bean.info.BindInfo;
import cn.sdormitory.sys.service.SysUserService;
import com.google.common.collect.Table;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component("studentMappingQuery")
public class StudentMappingQuery implements MappingQuery{

    @Resource
    private BStudentService studentService;

    @Override
    public Map<String, String> getMapping(BindInfo bindInfo, List<String> variables, Table<String, String, String> params) {
        List<Long> ids = ContainerUtil.splitToIds(variables);
        Map<Long, String> mappings = studentService.findIdAndNameMappings(ids);
        Map<String, String> result = ContainerUtil.toStrMap(mappings);
        if(ContainerUtil.isEmpty(result)){
            return result;
        }
        return ContainerUtil.toMapSafe(variables,
                Function.identity(),
                variable -> ContainerUtil.splitToStream(variable, ",")
                        .map(id -> result.getOrDefault(id, id))
                        .collect(Collectors.joining(","))
        );
    }
}
